﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcShop
{
    public static class StringExtensions
    {
        /// <summary>
        /// string.Format扩展
        /// </summary>
        /// <param name="format">原字符串</param>
        /// <param name="args">参数组</param>
        /// <returns></returns>
        public static string With(this string format, params object[] args)
        {
            return string.Format(format, args);
        }

        /*功能：删除字符串中指定的字符
        参数：str中存放字符串
        remove中存放要删除的字符*/
        public static string RemoveChars(string str, int location)
        {
            var strCha = str.ToCharArray();
            string result = String.Empty;
            for (int i = 0; i < str.Length - 1; i++)
            {
                if (i == location) break;
                result = result + strCha[i];
            }
            return result;
        }

        /// <summary>
        /// 将字符串ID传换成整数列ID
        /// </summary>
        /// <param name="id">字符串ID</param>
        /// <returns>整数列ID</returns>
        public static IEnumerable<int> ConvertIntList(string id)
        {
            List<int> result = new List<int>();
            var iList =id.Split(',');
            int temp;
            foreach (var x in iList)
            {
                if (Int32.TryParse(x, out temp))
                    result.Add(temp);
                else
                    break;
            }
            return result;
        }
    }
}
